package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;

import com.itheima.health.pojo.Members;
import com.itheima.health.service.ReportService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;

/**
 * 数据报告控制器
 */
@RestController
@RequestMapping("/report")
@Slf4j
@Api(tags = "数据报告展示接口")
public class ReportController {

    @Autowired
    ReportService reportService;

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        Map<Object,Object> data = reportService.getSetmealReport();
        log.info("[数据报告控制器]setmealCount:{}", data);
       return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,data);
    }

    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        Map<Object,Object> data = reportService.getBusinessReportData();

        log.info("[运营统计数据]setmealCount:{}", data);
        return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS,data);
    }

    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        LinkedHashMap<String,Integer> data = reportService.getZhexianData();
        List<String> months=new ArrayList<>();
        List<Integer> memberCount=new ArrayList<>();
        Set<String> set = data.keySet();
        for (String s : set) {
            months.add(s);
            Integer integer = data.get(s);
            memberCount.add(integer);
        }
        log.info("折线的数据", data);
        return new Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,new Members(months,memberCount));
    }

    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request,HttpServletResponse response)  {
        try {
            Map<Object,Object> data = reportService.getBusinessReportData();
            log.info("[exportBusinessReport运营统计数据]setmealCount:{}", data);

            List<Map<String, Object>> hotSetmeal = (List<Map<String, Object>>)data.get("hotSetmeal");
            log.info("hotSetmeal:{}", hotSetmeal);
            String reportDate = (String) data.get("reportDate");
            Integer todayNewMember = (Integer) data.get("todayNewMember");
            Integer totalMember = (Integer) data.get("totalMember");
            Integer thisWeekNewMember = (Integer) data.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) data.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) data.get("todayOrderNumber");
            Integer thisWeekOrderNumber = (Integer) data.get("thisWeekOrderNumber");
            Integer thisMonthOrderNumber = (Integer) data.get("thisMonthOrderNumber");
            Integer todayVisitsNumber = (Integer) data.get("todayVisitsNumber");
            Integer thisWeekVisitsNumber = (Integer) data.get("thisWeekVisitsNumber");
            Integer thisMonthVisitsNumber = (Integer) data.get("thisMonthVisitsNumber");
            //获得Excel表绝对路径,springboot结构不能获取项目绝对地址
            String temlateRealPath = request.getSession().getServletContext().getRealPath("resources") +
                    File.separator + "report_template.xlsx";
            //用类加载器的 读取资源的流
//            InputStream resourceAsStream = (this.getClass().getClassLoader().getResourceAsStream("report_template.xlsx"));
//            log.info("resourceAsStream:{}",resourceAsStream);
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
            XSSFSheet sheetAt = workbook.getSheetAt(0);

            XSSFRow row = sheetAt.getRow(2);
            row.getCell(5).setCellValue(reportDate);

            row = sheetAt.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);
            row.getCell(7).setCellValue(totalMember);

            row = sheetAt.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);
            row.getCell(7).setCellValue(thisMonthNewMember);

            row = sheetAt.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);
            row.getCell(7).setCellValue(todayVisitsNumber);

            row = sheetAt.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);
            row.getCell(7).setCellValue(thisWeekVisitsNumber);

            row = sheetAt.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);
            row.getCell(7).setCellValue(thisMonthVisitsNumber);

            Integer a = 12;
            for (Map<String, Object> map : hotSetmeal) {
                log.info("map:{}", map);
                String name = (String) map.get("name");
                log.info("name:{}", name);
                Double setmeal_count = (Double) map.get("setmeal_count");
                String proportion = (String) map.get("proportion");


                row = sheetAt.getRow(a++);
                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmeal_count);//预约数量
                row.getCell(6).setCellValue(proportion);//占比
            }
            log.info("for (Map<String, Object> map : hotSetmeal) ");

            //通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
            workbook.write(out);
            log.info("通过输出流进行文件下载");
            out.flush();
            out.close();
            workbook.close();
            log.info("释放完成");
            return null;
        } catch (IOException e) {
            return new Result(false,MessageConst.GET_BUSINESS_REPORT_FAIL);
        }

    }

}
